{extend name="admin@index_layout"/}
{block name="main"}
<div class="layui-card">
    <div class="layui-card-header">模型列表</div>
    <div class="layui-card-body">
        <table class="layui-hide" id="table" lay-filter="table"></table>
    </div>
</div>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <a class="layui-btn layui-btn-sm" href="{:url('cms/models/add')}">添加模型</a>
    </div>
</script>
<script type="text/html" id="barTool">
	<a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="field">字段管理</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script type="text/html" id="statusTpl">
    <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="开启|关闭" lay-filter="status" {{ d.status==1 ? 'checked' : '' }}>
</script>
<script type="text/html" id="ifsubTpl">
    <input type="checkbox" name="ifsub" value="{{d.id}}" lay-skin="switch" lay-text="允许|禁止" lay-filter="ifsub" {{ d.ifsub==1 ? 'checked' : '' }}>
</script>
{/block}
{block name="script"}
<script>
layui.use('table', function() {
    var table = layui.table,
        $ = layui.$,
        form = layui.form;
    table.render({
        elem: '#table',
        toolbar: '#toolbarDemo',
        url: '{:url("cms/models/index")}',
        cols: [
            [
                { field: 'id', width: 80, title: '模型ID' },
                { field: 'name', width: 120, title: '模型名称' },
                { field: 'tablename', width:120,title: '数据表' },
                { field: 'description', title: '描述' },
                { field: 'type', width:120,title: '类型',templet: '<div>{{#  if(d.type==1){ }} 独立表 {{#  } else { }} 主附表 {{#  } }} </div>' },
                { field: 'create_time',width:180, title: '创建时间' },
                { field: 'status', width: 100, title: '状态', templet: '#statusTpl', unresize: true },
                { field: 'ifsub', width: 100, title: '投稿', templet: '#ifsubTpl', unresize: true },
                { fixed: 'right', title: '操作', width: 200, templet: '#barTool' }
            ]
        ]
    });

        //监听状态操作
    form.on('switch(status)', function(obj) {
        var _this = $(obj.elem);
        $.post('{:url("cms/models/setstate")}', { 'id': this.value,'status':obj.elem.checked}, function(data) {
            if (data.code == 1) {
                layer.msg(data.msg);
            }else{
                !obj.elem.checked ? _this.prop('checked',true) : _this.removeAttr('checked');
                form.render('checkbox');
                layer.msg(data.msg);
            }

        });
    });

        //监听状态操作
    form.on('switch(ifsub)', function(obj) {
        var _this = $(obj.elem);
        $.post('{:url("cms/models/setSub")}', { 'id': this.value,'ifsub':obj.elem.checked}, function(data) {
            if (data.code == 1) {
                layer.msg(data.msg);
            }else{
                !obj.elem.checked ? _this.prop('checked',true) : _this.removeAttr('checked');
                form.render('checkbox');
                layer.msg(data.msg);
            }

        });
    });

    //监听行工具事件
    table.on('tool(table)', function(obj) {
        var data = obj.data;
        if (obj.event === 'del') {
            layer.confirm('确定删除这条数据？', { icon: 3, title: '提示' }, function(index) {
                layer.close(index);
                $.post('{:url("cms/models/delete")}', { 'id': data.id }, function(data) {
                    if (data.code == 1) {
                        if (data.url) {
                            layer.msg(data.msg + ' 页面即将自动跳转~');
                        } else {
                            layer.msg(data.msg);
                        }
                        setTimeout(function() {
                            if (data.url) {
                                location.href = data.url;
                            } else {
                                location.reload();
                            }
                        }, 1500);
                    } else {
                        layer.msg(data.msg);
                        setTimeout(function() {
                            if (data.url) {
                                location.href = data.url;
                            }
                        }, 1500);
                    }

                });
            });
        }else if (obj.event === 'edit') {
            window.open('{:url("cms/models/edit")}' + "?id=" + data.id, '_self')
        }else if (obj.event === 'field') {
            window.open('{:url("cms/field/index")}' + "?id=" + data.id, '_self')
        }
    });
});
</script>
{/block}